<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="false">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:650px;overflow:auto">
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="生产单号" prop="produce_no">
                                <el-input v-model="form.produce_no" placeholder="请输入生产单号" disabled></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="订单号" prop="order_no" >
                                <el-input v-model="form.order_no" placeholder="请输入订单号" :disabled="source==0"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="工厂货号" prop="factory_number">
                                <el-input v-model="form.factory_number" placeholder="请输入工厂货号" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="批次号" prop="batch_number">
                                <el-input v-model="form.batch_number" placeholder="请输入批次号" disabled></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="颜色" prop="colour" >
                                <el-input v-model="form.colour" placeholder="请输入skc" disabled></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="skc" prop="skc" >
                                <el-input v-model="form.skc" placeholder="请输入skc" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="商品款式" prop="style_id">
                                <el-select v-model="form.style_id" clearable style="width: 100%" placeholder="请选择" disabled>
                                    <el-option v-for="item in goodsList" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="客户交期" prop="deliver_time" >
                                <el-date-picker v-model="form.deliver_time" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择交期时间" disabled />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="客户名称" prop="client_id" >
                                <el-select v-model="form.client_id" clearable style="width: 100%" filterable placeholder="请选择" disabled>
                                    <el-option v-for="item in curList" :key="item.custom_id" :label="item.apply_name" :value="item.custom_id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="针种" prop="needle_seed">
                                <el-select v-model="form.needle_seed" clearable style="width: 100%" filterable placeholder="请选择" multiple>
                                    <el-option v-for="item in needleList" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="毛种" prop="wool_seed">
                                <el-input v-model="form.wool_seed" placeholder="毛种" readonly></el-input>
                                <!-- <el-select v-model="form.wool_seed" clearable style="width: 100%" placeholder="请选择">
                                    <el-option v-for="item in woolList" :key="item.id" :label="item.cate_name" :value="item.id" />
                                </el-select> -->
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="本厂大货跟单" prop="follow_id" >
                                <!-- <el-input v-model="form.follow_id" placeholder="请输入本厂大货跟单"></el-input> -->
                                <el-select v-model="form.follow_id" clearable style="width: 100%" placeholder="请选择" disabled>
                                    <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="8">
                            <el-form-item label="洗水方式" prop="washing_method">
                                <el-select v-model="form.washing_method" clearable style="width: 100%" filterable placeholder="请选择">
                                    <el-option v-for="item in washList" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="商品图片">
                                <el-image class="img" :src="form.goods_image_item[0]" :preview-src-list="form.goods_image_item" :initial-index="0" fit="cover" preview-teleported="true" hide-on-click-modal />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 订单明细 -->
                    <el-col :span="24">
                        <el-form-item label="订单明细" prop="size_data">
                            <sc-form-table ref="table" v-model="form.size_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                                <el-table-column prop="size" label="尺码"></el-table-column>
                                <el-table-column prop="num" label="数量">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.num" placeholder="请输入数量"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="produce_num" label="计划生产数">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.produce_num" @input="input(scope.row)" placeholder="请输入计划生产数"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="weight" label="生产克重/G">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.weight"  @input="input(scope.row)" placeholder="请输入生产克重"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="total_weight" label="生产总重量/KG">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.total_weight" readonly placeholder="请输入生产总重量"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="loom_time" label="织机时间" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.loom_time" placeholder="请输入织机时间">
                                            <template #append>分钟</template>
                                        </el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 物料配比表 -->
                    <el-col :span="12">
                        <el-form-item label="物料配比表" prop="produce_proportioning">
                            <sc-form-table ref="table" v-model="form.produce_proportioning" :addTemplate="produce_proportioning" placeholder="暂无数据" showSummary hideDelete @son="materAdds">
                                <el-table-column align="center" width="50">
                                    <template #default="scope">
                                        <el-button type="danger" icon="el-icon-delete" size="small" plain circle @click="del(scope.row.two_process_id,scope.$index)"></el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column label="工序" align="center">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.two_process_id" placeholder="请选择" clearable @change="materAdd(scope.row.two_process_id)">
                                            <el-option v-for="item in goodsProList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column label="是否主工序" align="center" width="120">
                                    <template #default="scope">
                                        <el-switch v-model="scope.row.is_main_process " :active-value="1" :inactive-value="0" @change="mainChange(form.produce_proportioning,scope.$index)"></el-switch>
                                    </template>
                                </el-table-column>
                                <el-table-column label="色位" align="center">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.color_position" placeholder="请选择" filterable @change="colorChange">
                                            <el-option v-for="item in colorPosition" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="ratio" label="比例" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.ratio" type="number" placeholder="请输入比例" @mousewheel.prevent></el-input>
                                    </template>
                                </el-table-column>
                                <!-- <el-table-column label="损耗" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.loss_ratio" type="number"  placeholder="请输入损耗" @input="proInput"></el-input>
                                    </template>
                                </el-table-column> -->
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 毛料 -->
                    <el-col :span="24">
                        <el-form-item label="毛料" prop="material_data">
                            <!-- <el-button type="primary" size="small" icon="el-icon-plus" @click="openWu">新增毛料</el-button> -->
                            <sc-form-table ref="table" v-model="form.material_data" :addTemplate="material" placeholder="暂无数据" hideAdd show-summary>
                                <el-table-column label="色位" align="center" width="80">
                                    <template #default="scope">
                                        {{ scope.row.color_position_title }}
                                        <!-- <el-input v-model="scope.row.color_position_title" placeholder="色位" readonly></el-input> -->
                                        <!-- <el-select v-model="scope.row.color_position" placeholder="请选择" filterable @change="matColorChange(scope.row)">
                                            <el-option v-for="item in Position" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select> -->
                                    </template>
                                </el-table-column>
                                <el-table-column align="center" width="50">
                                    <template #default="scope">
                                        <el-button v-if="!scope.row.material_spec_code" type="primary" icon="el-icon-plus" size="small" plain circle @click="openWu(scope.row,scope.$index)"></el-button>
                                        <el-button v-else type="warning" icon="el-icon-refresh" size="small" plain circle @click="openWu(scope.row,scope.$index)"></el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="skc" label="毛料skc" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.skc}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="material_name" label="物料名称" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.material_name}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="色号" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.color_number}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="colour" label="颜色" align="center">
                                    <template #default="scope">
                                        <div v-if="scope.row.colour">{{scope.row.colour}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="图片" align="center">
                                    <template #default="scope">
                                        <el-image style="width:50px;height:50px" :src="scope.row.image" :preview-src-list="scope.row.image" :initial-index="0" fit="cover" preview-teleported="true" hide-on-click-modal />
                                    </template>
                                </el-table-column>
                                <el-table-column prop="apply_name" label="供应商" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.apply_name}}</div>
                                    </template>
                                </el-table-column>
                                <!-- <el-table-column prop="ratio" label="比例（%）" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.ratio" placeholder="请输入比例（%）"></el-input>
                                    </template>
                                </el-table-column> -->
                                <el-table-column prop="loss_ratio" label="损耗比（%）" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.loss_ratio" placeholder="请输入损耗比（%）" type="number" @mousewheel.prevent @input="ratioInput(scope.row)"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="weight" label="所需总重量" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.weight" placeholder="请输入所需总重量" readonly></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column width="75" align="center">
                                    <template #default="scope">
                                        <el-button type="primary" size="small" @click="matColorChange(scope.row)">计算</el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column label="备注" align="center">
                                    <template #default="scope">
                                        <!-- <div v-if="scope.row.remark">{{scope.row.remark}}</div> -->
                                        <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 工序 -->
                    <el-col :span="24">
                        <el-form-item label="工序信息" prop="process_data">
                            <!-- <el-button type="primary" size="small" @click="openGong">选择工序</el-button> -->
                            <sc-form-table ref="table" v-model="form.process_data" :addTemplate="process" drag-sort placeholder="暂无数据" hideDelete>
                                <el-table-column width="65">
                                    <template #default="scope">
                                        <el-button type="primary" size="small" icon="el-icon-search" @click="openGong(scope.row,scope.$index)"></el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column align="center" width="50">
                                    <template #default="scope">
                                        <el-button type="danger" icon="el-icon-delete" size="small" plain circle @click="delPro(scope.row.two_process_id,scope.$index)"></el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="process_id" label="一级工序" align="center" width="250">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.process_id" @change="changeGong(scope.row)" placeholder="请选择" filterable>
                                            <el-option v-for="item in processList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="GongxuSon" label="二级工序" align="center" width="250">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.two_process_id" placeholder="请选择" filterable @change="twoProChange(scope.row)">
                                            <el-option v-for="item in scope.row.GongxuSon" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="remark" label="工序备注" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.remark"></el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 辅料 -->
                    <el-col :span="24">
                        <el-form-item label="辅料" prop="accessories_data">
                            <el-button type="primary" size="small" icon="el-icon-plus" @click="openWuFu">新增辅料</el-button>
                            <sc-form-table ref="table" v-model="form.accessories_data" :addTemplate="material" placeholder="暂无数据" hideAdd>
                                <el-table-column label="工序" align="center">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.two_process_id" placeholder="请选择" clearable>
                                            <el-option v-for="item in twiceList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="skc" label="辅料skc" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.skc}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="material_name" label="辅料名称" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.material_name}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="color_number" label="色号" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.color_number}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="colour" label="颜色" align="center">
                                    <template #default="scope">
                                        <div v-if="scope.row.colour">{{scope.row.colour}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="image" label="图片" align="center">
                                    <template #default="scope">
                                        <el-image style="width:50px;height:50px" :src="scope.row.image" :preview-src-list="scope.row.image" :initial-index="0" fit="cover" preview-teleported="true" hide-on-click-modal />
                                    </template>
                                </el-table-column>
                                <el-table-column prop="apply_name" label="供应商" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.apply_name}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="ratio" label="用量" align="center">
                                    <template #default="scope">
                                        <!-- <el-input v-model="scope.row.ratio" placeholder="请输入用量" @input="materialInput(scope.row)"></el-input> -->
                                        <el-input v-model="scope.row.ratio" placeholder="请输入用量" type="number" @mousewheel.prevent></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="unit" label="单位" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.unit" placeholder="请输入单位" readonly></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="loss_ratio" label="损耗" align="center">
                                    <template #default="scope">
                                        <!-- <el-input v-model="scope.row.loss_ratio" placeholder="请输入损耗" @input="materialInput(scope.row)"></el-input> -->
                                        <el-input v-model="scope.row.loss_ratio" placeholder="请输入损耗" type="number" @mousewheel.prevent></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="weight" label="所需总量" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.weight" placeholder="请输入所需总量" type="number" @mousewheel.prevent></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column width="75" align="center">
                                    <template #default="scope">
                                        <el-button type="primary" size="small" @click="counts(scope.row)">计算</el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="remark" label="备注">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 尺寸 -->
                    <el-col :span="24">
                        <el-form-item label="尺寸信息" prop="size">
                            <sc-form-table ref="table" v-model="size" :addTemplate="location" placeholder="暂无数据" hideAdd hideDelete>
                                <el-table-column prop="location_id" label="部位" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.location_name}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="size_latitude" label="尺寸纬度" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.size_latitude}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="measure_method" label="量法" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.measure_method}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="yangyi_size" label="样衣尺寸" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.yangyi_size}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="zhiyang_size" label="纸样尺寸" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.zhiyang_size}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="tiaoma" label="跳码" align="center" width="150">
                                    <template #default="scope">
                                        <div>{{scope.row.tiaoma}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="dahuo_size" label="大货尺寸" align="center">
                                    <el-table-column align="center" v-for="(child,index) in location.dahuo_size" :key="index" :label="child.size_name" :prop="child.size">
                                        <template #default="scope">
                                            <div>{{scope.row.dahuo_size[index].size}}</div>
                                        </template>
                                    </el-table-column>
                                </el-table-column>
                                <el-table-column prop="tolerance_range" label="允差范围" align="center">
                                    <template #default="scope">
                                        <div>{{scope.row.tolerance_range}}</div>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                    <!-- 资料管理 -->
                    <el-row :gutter="1">
                        <el-col :span="8">
                            <el-form-item label="吓数文件">
                                <sc-upload-file v-model="form.paper_sample_url" :limit="3" disabled>
                                    <el-button type="primary" icon="el-icon-upload" @click="paperUpload">吓数文件下载</el-button>
                                </sc-upload-file>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="画花文件">
                                <sc-upload-file v-model="form.ziliao_url" :limit="3" disabled>
                                    <el-button type="primary" icon="el-icon-upload" @click="ziliaoUpload">画花文件下载</el-button>
                                </sc-upload-file>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取消</el-button>
                    <el-button v-if="mode!='show'" :loading="isSaveing" @click="submit(0)">保存</el-button>
                    <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit(2)">保存并下物料单</el-button>
                </div>
            </el-footer>
        </el-container>
        <wu v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable :query="{category_id:type== 1 ? 1 : 2}"></wu>
        <gong v-if="dialog.gong" ref="gongDialog" @success="gongSuccess" @closed="dialog.gong = false" draggable></gong>
	</el-dialog>
</template>

<script>
import Gong from './gong'
import wu from './wu'
	export default {
        components: { wu,Gong },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '完善生产单',
					edit: '编辑生产单',
					show: '查看'
				},
                dialog: {
				    save: false,
                    gong:false
			    },
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					id:"",
					produce_no: "",
					colour: "",
					goods_image_item: "",
                    order_no: "",
                    factory_number: "",
                    batch_number: "",
                    washing_method : "",
                    skc: "",
                    style_id: "",
                    deliver_time: "",
                    client_id: "",
                    needle_seed:"",
                    wool_seed:"",
                    follow_id:"",
                    remark:"",
                    size_data:[],   //订单明细
                    material_data:[],   //毛料使用配比
                    accessories_data:[],   //辅料配比
                    process_data:[],   //工序
                    paper_sample_url:"",
                    ziliao_url:"",
                    material_purchase_status:"",
                    produce_proportioning:[],//物料配比表
				},
                size:[],
                material:{material_code:"",material_id:"",},
                process:{process_id:"",two_process_id:"",sort:"",price:"",remark:"",staff_id:""},
                location:{location_id:"",size_latitude:"",measure_method:"",yangyi_size:"",zhiyang_size:"",tiaoma:"",tolerance_range:"",dahuo_size:[]},
                produce_proportioning:{loss_ratio:0},
                allNum:0,//计划生产总数
                allWeight:0,//计划生产总重量
				//验证规则
				rules: {
                    washing_method : [
                        { required: true, message: '请选择洗水方式' }
                    ],
                },
				goodsList: [],  //商品款式
                curList:[],//客户
                needleList:[],//针种
                woolList:[],//毛种
                humList:[],//员工
                colorPosition:[],//色位
                washList:[],//洗水方式
                type:"",
                source:"",
                Position:[],
                newProcessList:[],
                newLoomTwoProcessList:[],
                newLoomTwoProcessIdList:[],
                twiceList:[],
			}
		},
		mounted() {
            this.getAllGoodsStyle()
            this.getCustomerList()
            this.getNeedle()
            this.getWool()
            this.getWash()
            this.getHum()
            this.getProcess()
            this.getColorPosition()
            this.getGoodsProcess()
            this.getTwiceProcess()
		},
		methods: {
            materAdds(){
                this.form.produce_proportioning.map(item=>{
                    item.is_main_process = 0
                })
            },
            // 配比change事件
            materAdd(){
                let arr = []
                this.form.produce_proportioning.map(item=>{
                    console.log('所有',item);
                    let tpo = '_' + item.two_process_id;
                    if(item.two_process_id){
                        if (arr.indexOf(item.two_process_id) == -1) {
                            arr.push(item.two_process_id)
                        }
                        
                        if (tpo in this.newProcessList) {
                            console.log('存在');
                        } else {
                            let proTemp = {
                                process_id: 3,
                                two_process_id: item.two_process_id,
                                GongxuSon: this.newLoomTwoProcessList,
                            };
                            this.form.process_data.push(proTemp);
                            this.newProcessList[tpo] = proTemp;
                        }
                    }
                })
                this.form.process_data.map((item,index)=>{
                    if(item.process_id == 3 && arr.indexOf(item.two_process_id) == -1){
                        delete this.newProcessList['_' + item.two_process_id]
                        this.form.process_data.splice(index,1)
                    }
                })
            },
            // 配比删除
            del(row,index){
                let array = []
                this.form.produce_proportioning.splice(index,1)
                this.form.produce_proportioning.map(produce=>{
                    array.push(produce.two_process_id)
                })
                this.form.process_data.map((item,index)=>{
                    if(row == item.two_process_id && array.indexOf(item.two_process_id) == -1){
                        this.form.process_data.splice(index,1)
                        delete this.newProcessList['_' + item.two_process_id]
                    }
                })
            },
            // 工序删除
            delPro(row,index){
                this.form.process_data.splice(index,1)
                if(this.newLoomTwoProcessIdList.indexOf(row) != -1){
                    delete this.newProcessList['_' + row]
                }
                let a = this.form.process_data.filter(e => e.process_id === 7).length;
                if(a==1){
                    this.form.process_data.map(item=>{
                        if(item.process_id == 7){
                            this.form.accessories_data.map(item1=>{
                                item1.two_process_id = item.two_process_id
                            })
                        }
                    })
                }else{
                    this.form.accessories_data.map(item=>{
                        item.two_process_id = ""
                    })
                }
            },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(e){
                this.form.process_data.map((item,index)=>{
                    item.sort = index
                })
                this.form.material_purchase_status = e
                if(this.form.material_data == ''){
                    this.$alert("毛料不能为空", "提示", {type: 'error'})
                    return
                }
                if(this.form.process_data == ''){
                    this.$alert("工序不能为空", "提示", {type: 'error'})
                    return
                }
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
                        if(this.mode=="edit"){
                            res = await this.$API.produce.getProduce.edit.post(this.form);
                        }else{
                            res = await this.$API.produce.getProduce.perfect.post(this.form);
                        }
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
                var res = await this.$API.produce.getProduce.detail.get({id:data.id});
                this.form.produce_no = res.data.produce_no
                this.source = res.data.source
                this.form.order_no = res.data.order_no
                this.form.factory_number = res.data.factory_number
                this.form.batch_number = res.data.batch_number
                this.form.washing_method = res.data.washing_method
                this.form.skc = res.data.skc
                this.form.colour = res.data.goods.colour
                this.form.goods_image_item = (res.data.goods.goods_image_item).split(',')
                console.log('this.form.goods_image_item :>> ', this.form.goods_image_item);
                this.form.style_id = res.data.style_id
                this.form.deliver_time = res.data.deliver_time
                this.form.client_id = res.data.client_id
                this.form.needle_seed = res.data.needle_seed
                this.form.wool_seed = res.data.wool_seed
                this.form.follow_id = res.data.follow_id
                this.form.remark = res.data.remark
                this.form.paper_sample_url = res.data.goods.paper_sample_url
                this.form.ziliao_url = res.data.goods.ziliao_url
                let matArr = []
                res.data.produce_material_proportioning.map(item=>{ 
                    matArr.push({
                        factory_number:res.data.factory_number,
                        two_process_id:item.two_process_id,
                        color_position:item.color_position,
                        ratio:item.ratio,
                        loss_ratio:item.loss_ratio,
                        is_main_process:item.is_main_process,
                    })
                })
                let ma = []
                matArr.map(item=>[
                    this.colorPosition.map(item1=>{
                        if(item.color_position==item1.id){
                            ma.push({
                                color_position:item1.id,
                                color_position_title:item1.title,
                            })
                        }
                    })
                ])
                this.form.produce_proportioning = matArr
                // 尺寸
                this.size = res.data.goodsSpec
                    res.data.goodsSpec.map(item=>{
                        let a = item.dahuo_size
                        this.location.dahuo_size = a
                    })
                // 订单明细size_data
                let orderDetail = []
                res.data.orderDetail.map(item=>{
                    orderDetail.push({
                        id:item.id,
                        size:item.goods_size,
                        num:item.num,
                        produce_num:res.data.planned_speed>0? item.produce_num:item.num,
                        weight:item.weight,
                        loom_time:item.loom_time,
                        total_weight:((Number(item.num) * Number(item.weight))/1000).toFixed(2)
                    })
                })
                this.form.size_data = orderDetail
                let t = 0;
                let y = 0;
                for (let i = 0; i < this.form.size_data.length; i++) {
                    t = Number(this.form.size_data[i].produce_num) + t;
                    y = Number(this.form.size_data[i].total_weight) + y;
                }
                this.allNum = t;
                this.allWeight = y
                // 辅料
                let accessData = []
                res.data.accessories_data.map(item=>{
                    accessData.push({
                        id:item.id,
                        material_spec_code:item.material_spec_code,
                        two_process_id:item.two_process_id,
                        ratio:item.ratio,
                        loss_ratio:item.loss_ratio,
                        remark:item.remark,
                        material_name:item.material_name,
                        skc:item.materialSpec.skc,
                        color_number:item.materialSpec.color_number,
                        colour:item.materialSpec.colour,
                        image:[item.materialSpec.image],
                        apply_name:item.materialSpec.apply_name,
                        // weight:item.weight,
                        weight:(item.ratio * this.allNum * (1+item.loss_ratio/100)).toFixed(2),
                        unit:item.unit,
                    })
                })
                this.form.accessories_data = accessData
                // 毛料
                let materialData = []
                res.data.material_data.map(item=>{
                    matArr.map(item1=>{
                        if(item.color_position==item1.color_position){
                            item.ratio = item1.ratio
                        }
                    })
                    this.matColorChange(item)
                    this.colorPosition.map(item1=>{
                        if(item.color_position==item1.id){
                            item.color_position_title = item1.title
                        }
                    })
                    materialData.push({
                        id:item.id,
                        material_spec_code:item.material_spec_code,
                        // ratio:item.ratio,
                        loss_ratio:item.loss_ratio,
                        remark:item.remark,
                        material_name:item.material_name,
                        skc:item.materialSpec.skc,
                        color_number:item.materialSpec.color_number,
                        colour:item.materialSpec.colour,
                        image:[item.materialSpec.image],
                        apply_name:item.materialSpec.apply_name,
                        weight:((item.ratio * this.allWeight/100) * (1+item.loss_ratio/100)).toFixed(2),
                        color_position:item.color_position,
                        color_position_title:item.color_position_title,
                    })
                })
                this.form.material_data = materialData
                // 初始话物料配比下的毛料
                ma.map(item1=>{
                    let flag = false;
                    res.data.material_data.map(item=>{
                        if(item.color_position==item1.color_position){
                            flag = true;
                        }
                    })

                    if(flag === false){
                        this.form.material_data.push({
                            color_position:item1.color_position,
                            color_position_title:item1.color_position_title,
                        })
                    }
                })
                // 工序
                let processData = []
                res.data.process.map(item=>{
                    processData.push({
                        id:item.id,
                        process_id:item.process_id,
                        two_process_id:item.two_process_id,
                        sort:item.sort,
                        price:item.price,
                        staff_id:item.staff_id,
                        remark:item.remark,
                        GongxuSon:item.GongxuSon
                    })
                    this.newProcessList['_' + item.two_process_id] = item;
                    if (item.process_id == 3) {
                        this.newLoomTwoProcessList = item.GongxuSon;
                        this.newLoomTwoProcessIdList = item.GongxuSon.map(item1=>{
                            return item1.id
                        })
                    }
                })
                this.form.process_data = processData
                let a = this.form.process_data.filter(e => e.process_id === 7).length;
                if(a==1){
                    this.form.process_data.map(item=>{
                        if(item.process_id == 7){
                            this.form.accessories_data.map(item1=>{
                                item1.two_process_id = item.two_process_id
                            })
                        }
                    })
                }
                this.colorPosition.map(item=>{
                    this.form.produce_proportioning.map(item1=>{
                        if(item.id == item1.color_position){
                            this.Position.push({
                                id:item1.color_position, 
                                title:item.title,
                            })
                        }
                    })
                })
			},
            // 获取款式
            async getAllGoodsStyle(){
				var res = await this.$API.goods.getAllGoodsStyle.get();
				this.goodsList = res.data;
			},
            // 获取客户
            async getCustomerList(){
				var res = await this.$API.all.customer.get();
				this.curList = res.data;
			},
            //所有针种
            async getNeedle(){
                var res = await this.$API.all.needle.get();
                this.needleList = res.data;
            },
            // 毛种
            async getWool(){
                var res = await this.$API.basic.apply.getInfo.get({pid:1});
                this.woolList = res.data;
            },
            // 洗水方式
            async getWash(){
                var res = await this.$API.all.wash.get();
                this.washList = res.data;
            },
            // 跟单人
            async getHum(){
                var res = await this.$API.all.staff.get({job_code:"003"});
                this.humList = res.data;
            },
            // 所有工序
            async getProcess(){
                var res = await this.$API.all.process.get({pid:0});
                this.processList = res.data;
            },
            // 二级工序
            async changeGong(e){
                var res = await this.$API.all.process.get({pid:e.process_id});
                e.GongxuSon = res.data
            },
            // 色位
            async getColorPosition(){
                var res = await this.$API.all.colorPosi.get();
                this.colorPosition = res.data;
            },
            // 商品工序
            async getGoodsProcess(){
                var res = await this.$API.all.getGoodsPro.get();
                this.goodsProList = res.data;
            },
            async getTwiceProcess(){
                var res = await this.$API.all.process.get({pid:7});
                this.twiceList = res.data;
            },
            // 毛料
            openWu(row,index){
                this.type = 1
                this.dialog.save = true
                this.$nextTick(() => {
                    // this.$refs.saveDialog.open('add').setData({type:1})
                    let a = {
                        row:row,
                        index:index
                    }
                    this.$refs.saveDialog.open('add').setData(a)
                })
            },
            // 辅料
            openWuFu(row,index){
                this.type = 2
                this.dialog.save = true
                this.$nextTick(() => {
                    // this.$refs.saveDialog.open('add').setData({type:2})
                    let a = {
                        row:row,
                        index:index
                    }
                    this.$refs.saveDialog.open('add').setData(a)
                })
            },
            handleSuccess(data, mode,index,row) {
                let a = []
                let b = []
                data.map((item)=>{
                    a.push({
                        material_id:item.material.material_id,
                        material_spec_code:item.spec_code,
                        skc:item.skc,
                        category_name:item.material.category_name,
                        material_name:item.material.material_name,
                        color_number:item.color_number,
                        colour:item.colour,
                        image:item.image_arr,
                        apply_name:item.material.apply_name,
                        remark:item.remark,
                        unit:item.material.unit_name,
                        color_position:row.color_position,
                        color_position_title:row.color_position_title,
                        loss_ratio:item.material.loss_ratio
                    })
                })
                let existingIds = this.form.material_data.map(item => item.material_spec_code);
                for(let i = a.length - 1; i >= 0; i--){
                    if(existingIds.includes(a[i].material_spec_code)){
                        a.splice(i, 1);
                    }
                }
                let existingIds1 = this.form.accessories_data.map(item => item.material_spec_code);
                for(let i = a.length - 1; i >= 0; i--){
                    if(existingIds1.includes(a[i].material_spec_code)){
                        a.splice(i, 1);
                    }
                }
                if(mode==1){
                    if(a[0] !=undefined){
                        this.form.material_data[index] = a[0]
                        this.matColorChange(this.form.material_data[index])
                    // }else {
                    //     this.$alert('选择了重复的物料', "提示", {type: 'error'})
                    }
                }else{
                    this.form.accessories_data = [...this.form.accessories_data,...a]
                }
                this.form.material_data.map(item=>{
                    if(b.indexOf(item.material_name) == -1) {
                        b.push(item.material_name)
                    }

                })
                this.form.wool_seed = b.join("+")
            },
            openGong(row,index){
                this.dialog.gong = true
                this.$nextTick(() => {
                    this.$refs.gongDialog.open('add').setData({index:index})
                })
            },
            gongSuccess(data, mode) {
                console.log('mode :>> ', mode);
                this.form.process_data = []
                data[0].detail.map(item=>{
                    this.form.process_data.push({
                        process_id:item.process_id,
                        process_title:item.process_title,
                        price:item.price,
                        sort:item.sort,
                        remark:item.remark,
                        GongxuSon:item.GongxuSon,
                        staff_id:"",
                        two_process_id:item.two_process_id
                    })
                })
            },
            input(row){
                row.total_weight = ((Number(row.produce_num) * Number(row.weight))/1000).toFixed(2);
                let t = 0;
                let y = 0;
                for (let i = 0; i < this.form.size_data.length; i++) {
                    t = Number(this.form.size_data[i].produce_num) + t;
                    y = Number(this.form.size_data[i].total_weight) + y;
                }
                this.allNum = t;
                this.allWeight = y
                this.form.material_data.map(item=>{
                    item.weight = ((item.ratio * this.allWeight/100) * (1+item.loss_ratio/100)).toFixed(2)
                })
                this.form.accessories_data.map(item=>{
                    item.weight = (item.ratio * this.allNum * (1+item.loss_ratio/100)).toFixed(2)
                })
            },
            // 毛料计算
            count(row){
                // row.weight = ((row.ratio * this.allWeight/100) * (1+row.loss_ratio/100)).toFixed(2)
                this.form.produce_proportioning.map(item=>{
                    if(item.color_position == row.color_position){
                        this.form.material_data.map(item1=> {
                            if(item.color_position == item1.color_position){
                                row.weight = ((item.ratio * this.allWeight/100) * (1+item.loss_ratio/100)).toFixed(2)
                            }
                        })
                    }
                })
            },
            // 辅料计算
            counts(row){
                row.weight = (row.ratio * this.allNum * (1+row.loss_ratio/100)).toFixed(2)
            },
            paperUpload() {
                // 下载文件
                const fileUrl = this.form.paper_sample_url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = this.form.paper_sample_url;
                link.click();
            },
            ziliaoUpload() {
                // 下载文件
                const fileUrl = this.form.ziliao_url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = this.form.ziliao_url;
                link.click();
            },
            colorChange(){
                let a = []
                this.colorPosition.map(item => {
                    this.form.produce_proportioning.map(item1 => {
                        if (item.id == item1.color_position) {
                            a.push({
                                id: item1.color_position,
                                title: item.title,
                            })
                        }
                    })
                })
                this.Position = a
                // 创建一个临时数组来存储有效的 color_position id
                let validIds = this.Position.map(item => item.id)
                // 过滤 this.form.material_data，删除无效的数据
                this.form.material_data = this.form.material_data.filter(item => (validIds.includes(item.color_position) || item.material_spec_code))
                // 确保所有有效的 color_position 都在 this.form.material_data 中
                this.Position.forEach(item => {
                    if (!this.form.material_data.some(material => material.color_position === item.id)) {
                        this.form.material_data.push({
                            color_position: item.id,
                            color_position_title: item.title,
                        })
                    }
                })
            },
            matColorChange(row){
                let test = this.form.produce_proportioning
                let obj = {}
                let newArray = []
                test.forEach(item=>{
                    const ratio = parseFloat(item.ratio);
                    if(obj[item.color_position]){
                        obj[item.color_position].ratio += ratio;
                    }else{
                        obj[item.color_position] = { ...item,ratio };
                    }
                })
                newArray = Object.values(obj);
                newArray .map(item=>{
                    if(item.color_position == row.color_position){
                        this.form.material_data.map(item1=> {
                            if(item.color_position == item1.color_position){
                                row.weight = ((item.ratio * this.allWeight/100) * (1+item1.loss_ratio/100)).toFixed(2)
                            }
                        })
                    }
                })
            },
            twoProChange(row){
                console.log('row :>> ', row);
                if(this.newLoomTwoProcessIdList.indexOf(row.two_process_id) != -1){
                    let proTemp = {
                        process_id: 3,
                        two_process_id: row.two_process_id,
                        GongxuSon: this.newLoomTwoProcessList,
                    };
                    this.newProcessList['_' + row.two_process_id] = proTemp;
                }
                let a = this.form.process_data.filter(e => e.process_id === 7).length;
                if(a==1){
                    this.form.process_data.map(item=>{
                        if(item.process_id == 7){
                            this.form.accessories_data.map(item1=>{
                                item1.two_process_id = item.two_process_id
                            })
                        }
                    })
                }else{
                    this.form.accessories_data.map(item=>{
                        item.two_process_id = ""
                    })
                }
            },
            ratioInput(row){
                this.matColorChange(row)
            },
            mainChange(row,index){
                for (let i = 0; i < row.length; i++) {
                    row[i].is_main_process = 0;
                }
                row[index].is_main_process = 1;
            }
		}
	}
</script>

<style lang="scss">
.img{
    width: 250px;
    height: 100%;
}
</style>
